package com.easy.securityOauth2AuthCodeServer.service;


import com.easy.securityOauth2AuthCodeServer.model.UserDetailsExpand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author kdyzm
 */
//@Slf4j
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

//    @Autowired
//    private UserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("---------用户： "+username);
//        TUser tUser = userMapper.selectOne(username);
//        if (Objects.isNull(tUser)) {
//            throw new UsernameNotFoundException(username + "账号不存在");
//        }
//        System.out.println("---------用户： "+tUser.getAccount());
        /*List<String> allPermissions = userMapper.findAllPermissions(tUser.getId());
        String[] array = null;
        if (CollectionUtils.isEmpty(allPermissions)) {
            log.warn("{} 无任何权限", tUser.getUsername());
            array = new String[]{};
        } else {
            array = new String[allPermissions.size()];
            allPermissions.toArray(array);
        }
        UserDetailsExpand userDetailsExpand = new UserDetailsExpand(tUser.getUsername(), tUser.getPassword(), AuthorityUtils.createAuthorityList(array));
        userDetailsExpand.setId(tUser.getId());
        userDetailsExpand.setEmail(tUser.getEmail());
        userDetailsExpand.setMobile(tUser.getMobile());
        userDetailsExpand.setFullname(tUser.getFullname());*/
       UserDetailsExpand userDetailsExpand = new UserDetailsExpand("admin","123456", AuthorityUtils.createAuthorityList(new String[]{}));
//        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//        // 创建两个内存用户
//        manager.createUser(User.withUsername("admin").password("123456").authorities("USER").build());
//        manager.createUser(User.withUsername("lin").password("123456").authorities("USER").build());
        return userDetailsExpand;
    }
}
